package br.usjt.mazerobot;

import br.usjt.mazerobot.commons.navegacao.Celula;

public class TabelaResponsavel {
    
    private Elemento[] tabela = new Elemento[20];
    private int ultimoIndice;

    public Celula consultaResponsavel(Celula pesquisada) {

        Elemento coordenada = new Elemento(pesquisada.getX(), pesquisada.getY());

        for (int i = 0; i < tabela.length; i++) {
            if (tabela[i].equals(coordenada)) {
                return new Celula(tabela[i].responsavelX, tabela[i].responsavelY);
            }
        }

        return null;
    }

    public void adicionaResponsavel(Celula coordenada, Celula responsavel) {

        if (ultimoIndice == tabela.length - 1) {
            redimensionar();
        }
        tabela[ultimoIndice++] = new Elemento(coordenada.getX(), coordenada.getY(), responsavel.getX(), responsavel.getY());
    }

    private void redimensionar() {
        int novoTamanho = (int) (tabela.length * 1.3);//Aumenta 30%
        Elemento[] auxiliar = new Elemento[novoTamanho];
        System.arraycopy(tabela, 0, auxiliar, 0, tabela.length);
        tabela = auxiliar;
    }
    
    
    private class Elemento {

        private short coordenadaX;
        private short coordenadaY;
        private short responsavelX;
        private short responsavelY;

        public Elemento(short coordenadaX, short coordenadaY, short responsavelX, short responsavelY) {
            this.coordenadaX = coordenadaX;
            this.coordenadaY = coordenadaY;
            this.responsavelX = responsavelX;
            this.responsavelY = responsavelY;
        }

        public Elemento(short coordenadaX, short coordenadaY) {
            this.coordenadaX = coordenadaX;
            this.coordenadaY = coordenadaY;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            final Elemento other = (Elemento) obj;
            if (this.coordenadaX != other.coordenadaX) {
                return false;
            }
            if (this.coordenadaY != other.coordenadaY) {
                return false;
            }
            return true;
        }

        @Override
        public int hashCode() {
            int hash = 5;
            hash = 17 * hash + this.coordenadaX;
            hash = 17 * hash + this.coordenadaY;
            return hash;
        }
    }
}